#include "MsgNode.h"
#include "../log.h"

#include "string.h"

#include <boost/asio/detail/socket_ops.hpp>



int HeadNode::GetHeatInfo()
{
    if(_data.get() == nullptr)
    {
        log_error("MsgNode::GetHeatInfo m_data is nullptr");
        return -1;
    }

    if(_total_len < HEAD_LENGTH)
    {
        log_error("MsgNode::GetHeatInfo m_total_len:{}, HEAD_LENGTH:{}", _total_len, HEAD_LENGTH);
        return -2;
    }

    memcpy(&_msg_id, _data.get(), ID_LENGTH);
    memcpy(&_msg_len, _data.get() + ID_LENGTH, MSG_LENGTH);
    _msg_id = boost::asio::detail::socket_ops::network_to_host_long(_msg_id);
    _msg_len = boost::asio::detail::socket_ops::network_to_host_long(_msg_len);
    log_debug("msg_id:{}, msg_len:{}", _msg_id, _msg_len);

    if(_msg_len > MSG_MAX_LENGTH)
    {
        log_error("MsgNode::GetHeatInfo msg_len:{} > MSG_MAX_LENGTH:{}", _msg_len, MSG_MAX_LENGTH);
        return -3;
    }

    return 0;
}

void HeadNode::Clear()
{
    MsgNode::Clear();
    _msg_id = 0;
    _msg_len = 0;
}


SendNode::SendNode(const char* msg, uint32_t msg_len, uint32_t msg_id)
:MsgNode(msg_len + HEAD_LENGTH)
,_msg_id(msg_id)
{
    uint32_t msg_id_net = boost::asio::detail::socket_ops::host_to_network_long(_msg_id);
    uint32_t msg_len_net = boost::asio::detail::socket_ops::host_to_network_long(msg_len);
    memcpy(_data.get(), &msg_id_net, ID_LENGTH);
    memcpy(_data.get() + ID_LENGTH, &msg_len_net, MSG_LENGTH);
    memcpy(_data.get() + HEAD_LENGTH, msg, msg_len);
}